import {
  HttpException,
  HttpStatus,
  Injectable,
  NestMiddleware,
} from "@nestjs/common";
import { Request, Response, NextFunction } from "express";
import { ResponseResult } from "../tools/response.result";

@Injectable()
export class TenantMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    const tenantId = req.headers["tenant-id"] as string;
    console.log("TenantMiddleware", tenantId);
    if (!tenantId) {
      return res
        .status(400)
        .send(new ResponseResult(1, "请求头参数错误,租户编号不能为空."));
      // req["tenant-id"] = tenantId;
    }
    req["tenant-id"] = tenantId;
    next();
  }
}
